/**
 * 质控病种字典管理初始化
 */
var SdsQcentity = {
    id: "SdsQcentityTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsQcentity.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '病种ID', field: 'id', width: 100, align: 'center', valign: 'middle', sortable: true, visible: false},
        {title: '缩写', field: 'abbrev', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '名称', field: 'btDesc', width: 200, align: 'center', valign: 'middle', sortable: true},
        {title: '接口地址', field: 'url', width: 200, align: 'center', valign: 'middle', sortable: true},
        {title: '发布时间', field: 'pubDate', width: 120, align: 'center', valign: 'middle', sortable: true},
        {title: '参考费用', field: 'cost', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '参考住院日', field: 'inDays', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '手术关键字', field: 'operKey', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '病种类型', field: 'entityType.description', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '病种状态', field: 'isActive', width: 100, align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
                if (value == 0) {
                    return "<span class='label label-warning'>作废</span>";
                } else if (value == 1) {
                    return "<span class='label label-success'>有效</span>";
                } else if (value == 2){
                    return "<span class='label label-info'>测试</span>";
                } else if (value == 3){
                    return "<span class='label label-default'>灰色</span>";
                }
            }
        },
        {title: '关联科室', field: 'linkDept', width: 100, align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index) {
                var deptMap = $.parseJSON($("#deptMap").val());
                var str="";
                for (var key in deptMap) {
                    if (value.indexOf(key) > -1) {
                        str += deptMap[key] + "<br>";
                    }
                }
                return str;
            }
        },
        {title: '入组序号', field: 'indNo', width: 100, align: 'center', valign: 'middle', sortable: true},
        {title: '当前版本', field: 'versionId', width: 100, align: 'center', valign: 'middle', sortable: true, visible:false},
        {title: '疾病系统', field: 'entityCat.description', width: 100, align: 'center', valign: 'middle', sortable: true}
        ];
};

/**
 * 检查是否选中
 */
SdsQcentity.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsQcentity.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增质控病种字典
 */
SdsQcentity.add = function () {
    var index = Sys.open('新增质控病种字典',Sys.backendPath + '/sds/sdsqcentity/to_add',['800px', '420px']);
    this.layerIndex = index;
};

/**
 * 打开查看质控病种字典详情
 */
SdsQcentity.edit = function (r) {
	var flag = false;
    var id = 0;
	if(r){
        id = r.id;
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
    	var index = Sys.open('质控病种字典详情',Sys.backendPath + '/sds/sdsqcentity/to_update/' + id,['800px', '420px']);
        this.layerIndex = index;
    }
};

/**
 * 删除质控病种字典
 */
SdsQcentity.del = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/sds/sdsqcentity/delete", function (data) {
	            Sys.success("删除成功!");
	            SdsQcentity.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("sdsQcentityId",id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除质控病种字典?",operation);
    }
};

/**
 * 查询质控病种字典列表
 */
SdsQcentity.search = function () {
    var queryData = {};
    queryData['entityCatId'] = $("#entityCatId").combobox("getValue");
    queryData['btDesc'] = $("#btDesc").val();
    SdsQcentity.table.refresh({query: queryData, pageNumber: 1});
};

SdsQcentity.expEntities = function () {
    Sys.loading("导出中。。。");
    var $df = $("#downForm");
    //提交表单，实现下载
    $df.submit();
    Sys.closeLoading();
};

SdsQcentity.impEntities = function () {
    var index = layer.open({
        type: 2,
        title: '导入病种',
        area: ['550px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/sds/sdsqcentity/to_import'
    });
    this.layerIndex = index;
};

SdsQcentity.template = function () {
    window.location.href = Sys.backendPath+"/static/backend/sds/sdsqcentity/tmp/QCEntity_Template.xlsx";
};

$(function () {
    var defaultColunms = SdsQcentity.initColumn();
    var table = new BSTable(SdsQcentity.id, Sys.backendPath + "/sds/sdsqcentity/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(15);
    table.setOnDblClickRow(SdsQcentity.edit);
    SdsQcentity.table = table.init();
});
